package com.wj.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wj.crm.base.BaseService;
import com.wj.crm.dao.CusDevPlanMapper;
import com.wj.crm.dao.SaleChanceMapper;
import com.wj.crm.query.CusDevPlanQuery;
import com.wj.crm.utils.AssertUtil;
import com.wj.crm.vo.CusDevPlan;
import com.wj.crm.vo.SaleChance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WangJin
 * @version 1.0
 * @date 2021/12/28 15:51
 */
@Service
public class CusDevPlanService extends BaseService<CusDevPlan,Integer> {

    @Autowired
    private CusDevPlanMapper cusDevPlanMapper;

    @Autowired
    private SaleChanceMapper saleChanceMapper;

    public Map<String,Object> queryCusDevPlanByParams(CusDevPlanQuery cusDevPlanQuery){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(cusDevPlanQuery.getPage(), cusDevPlanQuery.getLimit());
        PageInfo<CusDevPlan> pageInfo = new PageInfo<>(cusDevPlanMapper.selectByParams(cusDevPlanQuery));
        map.put("code",0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    /**
     * 添加计划项
     * is_valid
     * crateDate
     * updateDate
     * 3.
     */

    @Transactional(propagation = Propagation.REQUIRED)
    public void addCusDevPlan(CusDevPlan cusDevPlan){
        //1. 参数校验
        checkCusDevPlanParams(cusDevPlan);
        //2. 设置参数默认值
        //是否有效，默认有效
        cusDevPlan.setIsValid(1);
        //创建时间 系统当前时间
        cusDevPlan.setCreateDate(new Date());
        //修改时间 系统当前时间
        cusDevPlan.setUpdateDate(new Date());
        //3. 执行添加，判断结果
        AssertUtil.isTrue(cusDevPlanMapper.insertSelective(cusDevPlan) != 1,"计划数据项添加失败!");
    }

    private void checkCusDevPlanParams(CusDevPlan cusDevPlan){
        //营销机会ID 非空 记录必须存在
        Integer saleChanceId = cusDevPlan.getSaleChanceId();
        AssertUtil.isTrue(null == saleChanceId || saleChanceMapper.selectByPrimaryKey(saleChanceId) == null,"数据异常,请重试!");
        //计划项内容 非空
        AssertUtil.isTrue(StringUtils.isBlank(cusDevPlan.getPlanItem()),"计划内容不能为空!");
        //计划项时间 非空
        AssertUtil.isTrue(null == cusDevPlan.getPlanDate(),"计划时间不能为空!");
    }

    /**
     * 更新计划项
     * 1.参数校验
     * id 非空 记录存在
     * 营销机会id 非空 记录必须存在
     * 计划项内容 非空
     * 计划项时间 非空
     * 2.参数默认值设置
     * updateDate
     * 3.执行更新 判断结果
     */
    /**
     * 更新计划项
     * @param cusDevPlan
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateCusDevPlan(CusDevPlan cusDevPlan){
        AssertUtil.isTrue(null == cusDevPlan.getId() ||
                        null == selectByPrimaryKey(cusDevPlan.getId()),"待更新记录不存在!");
        // 1. 参数校验
        checkCusDevPlanParams(cusDevPlan);
        // 2. 设置参数默认值
        cusDevPlan.setUpdateDate(new Date());
        // 3. 执行添加，判断结果
        AssertUtil.isTrue(updateByPrimaryKeySelective(cusDevPlan) != 1,"计划项记录更新失败!");
    }

    /**
     * 删除计划项
     * @param id
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void delCusDevPlan(Integer id){
        CusDevPlan cusDevPlan = selectByPrimaryKey(id);
        AssertUtil.isTrue(null==id || null ==cusDevPlan ,"待删除记录不存在!");
        cusDevPlan.setIsValid(0);
        AssertUtil.isTrue(updateByPrimaryKeySelective(cusDevPlan) != 1,"计划项记录删除失败!");
    }


}
